package com.kly.order.db.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.google.common.collect.Maps;
import com.kly.enums.OrderStatus;
import com.kly.order.ServiceUtils;
import com.kly.order.db.dao.TOrderDao;
import com.kly.order.db.entity.TOrderEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class TOrderDaoImpl extends AbstractBaseMapper<TOrderEntity> implements TOrderDao {

    @Override
    public int updateByOrderNo(TOrderEntity params) {
        return this.getSqlSession().update(this.getStatement("updateByOrderNo"), params);
    }

    @Override
    public List<TOrderEntity> selectByToken(String purchaseToken, String appCode) {
        final HashMap<String, String> params = Maps.newHashMap();
        params.put("purchaseToken", purchaseToken);
        params.put("appCode", appCode);
        return this.getSqlSession().selectList(this.getStatement("selectByToken"), params);
    }
    @Override
    public TOrderEntity queryLatestSubscriptionsOrderByUserCode(String userCode) {
        return this.getSqlSession().selectOne(this.getStatement("queryLatestSubscriptionsOrderByUserCode"), userCode);
    }

    @Override
    public TOrderEntity selectLatestOrderByToken(String purchaseToken) {
        return this.getSqlSession().selectOne(this.getStatement("selectLatestOrderByToken"), purchaseToken);
    }

    @Override
    public TOrderEntity selectPaidOrderByPurchase(String userCode, Integer purchaseType) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("purchaseType", purchaseType);
        return this.getSqlSession().selectOne(this.getStatement("selectPaidOrderByPurchase"), params);
    }

    @Override
    public TOrderEntity queryByOrderNo(String orderNo) {
        return this.getSqlSession().selectOne(this.getStatement("queryByOrderNo"), orderNo);
    }

    @Override
    public List<TOrderEntity> listPaidOrder() {
        return this.getSqlSession().selectList(this.getStatement("listPaidOrder"));
    }

    @Override
    public Integer countByStatus(String userCode, Integer status) {
        Map<String, Object> params = ServiceUtils.getParams();
        params.put("userCode", userCode);
        params.put("status", status);
        return this.getSqlSession().selectOne(this.getStatement("countByStatus"), params);
    }
}